package com.miyu.client1.utils.commonMav;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

@Controller
public class bookComMav {

    /**
     * 处理页面跳转 以及参数传递
     * 固定写法 前/forward/分割 后转页面路径
     * 参数通过 request 接搜 key 以data发送
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forward/**",method = RequestMethod.GET)
    public ModelAndView forward(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String url = request.getRequestURI();
         url = url.substring(9,url.length());
        Enumeration<String> parameterNames = request.getParameterNames();
        JSONObject jo = new JSONObject();
       for(Enumeration e=parameterNames;e.hasMoreElements();){
           String thisName=e.nextElement().toString();
           String thisValue=request.getParameter(thisName);
          jo.put(thisName,thisValue);
       }
        mav.addObject("data",jo.toString());
        mav.setViewName(url);

        return mav;
    }

    /**
     * 处理页面跳转 以及参数传递
     * 固定写法 前/forward/分割 后转页面路径
     * 参数通过 request 接搜 key 以data发送
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forward/**",method = RequestMethod.POST)
    public ModelAndView fop(HttpServletRequest request,@RequestBody String jsonObject) throws Exception {
        String ds = request.getParameter("content");

        ModelAndView mav = new ModelAndView();
        String url = request.getRequestURI();
        url = url.substring(9,url.length());
        Enumeration<String> parameterNames = request.getParameterNames();
        JSONObject jo = new JSONObject();
        jo = JSONObject.parseObject(ds);
/*        for(Enumeration e=parameterNames;e.hasMoreElements();){
            String thisName=e.nextElement().toString();
            String thisValue=request.getParameter(thisName);
            jo.put(thisName,thisValue);
        }*/
        mav.addObject("data",jo.toString());
        mav.setViewName(url);

        return mav;
    }

    /**
     * 菜单处理页面跳转 以及get参数传递
     * 固定写法 前/forward/分割 后转页面路径
     * 参数通过 request 接搜 key 以data发送
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/meun/**",method = RequestMethod.GET)
    public ModelAndView meun(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String url = request.getRequestURI();
        url = url.substring(6,url.length());
        Enumeration<String> parameterNames = request.getParameterNames();
        JSONObject jo = new JSONObject();
        for(Enumeration e=parameterNames;e.hasMoreElements();){
            String thisName=e.nextElement().toString();
            String thisValue=request.getParameter(thisName);
            jo.put(thisName,thisValue);
        }
        mav.addObject("data",jo.toString());
        mav.setViewName(url);

        return mav;
    }
    /**
     * 菜单处理页面跳转 以及POST参数传递
     * 固定写法 前/forward/分割 后转页面路径
     * 参数通过 request 接搜 key 以data发送
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/meun/**",method = RequestMethod.POST)
    public ModelAndView meunPost(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String url = request.getRequestURI();
        url = url.substring(6,url.length());
        Enumeration<String> parameterNames = request.getParameterNames();
        JSONObject jo = new JSONObject();
        for(Enumeration e=parameterNames;e.hasMoreElements();){
            String thisName=e.nextElement().toString();
            String thisValue=request.getParameter(thisName);
            jo.put(thisName,thisValue);
        }
        mav.addObject("data",jo.toString());
        mav.setViewName(url);

        return mav;
    }
}
